package com.flaremars.picker.fragments;


import android.support.v7.widget.RecyclerView;

import com.flaremars.picker.adapters.PostItemAdapter;
import com.flaremars.picker.constants.ProjectConstants;
import com.flaremars.picker.entities.PostItem;
import com.flaremars.picker.fragments.common.BaseListFragment;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author FlareMars
 * 帖子页面
 */
public class BBSFragment extends BaseListFragment<PostItem> {

    public BBSFragment() {

    }

    @Override
    protected RecyclerView.Adapter setupAdapter(List<PostItem> dataObjects) {
        return new PostItemAdapter(getContext(),getContentRecyclerView(),dataObjects);
    }

    @Override
    protected String setupDataRequestUrl() {
        return ProjectConstants.BASE_URL + "bbs/getContent";
    }

    @Override
    protected List<PostItem> parseJson(String jsonStr) {
        List<PostItem> list = new ArrayList<>();
        try {
            //获取通用信息
            JSONObject rootJson = new JSONObject(jsonStr);
            Integer statusCode = rootJson.getInt("statusCode");
            String msg = rootJson.getString("message");

            //处理无数据情况
            if (statusCode.equals(ProjectConstants.STATUS_CODE_ERROR)) {
                changeFragmentStatus(FragmentStatus.REQUEST_ERROR);
                return list;
            } else if (statusCode.equals(ProjectConstants.STATUS_CODE_NO_MORE_DATA)) {
                changeFragmentStatus(FragmentStatus.NO_DATA);
                return list;
            }

            //处理分页信息
            JSONObject data = rootJson.getJSONObject("data");
            int curPage = data.getInt("curPage");
            int totalPage = data.getInt("totalPage");
            updatePageNum(curPage,totalPage);

            //解析数据列表
            JSONArray objectsJson = data.getJSONArray("objects");
            for (int i = 0;i < objectsJson.length();i++) {
                JSONObject temp = objectsJson.getJSONObject(i);
                PostItem tempEntity = new PostItem();
                tempEntity.setTime(new Date(temp.getLong("time")));
                tempEntity.setAvatar("");
                tempEntity.setContent(temp.getString("content"));
                List<String> urls = new ArrayList<>();
                JSONArray urlJsons = temp.getJSONArray("pictures");
                for (int j = 0;j < urlJsons.length();j++) {
                    urls.add(urlJsons.getString(j));
                }
                tempEntity.setPictures(urls);
                tempEntity.setPostId(temp.getString("postId"));
                tempEntity.setUsername(temp.getString("username"));
                tempEntity.setUserId(temp.getString("userId"));

                list.add(tempEntity);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    protected Map<String, String> setupUrlParams() {
        Map<String,String> paramsMap = new HashMap<>();
        paramsMap.put("curPage",String.valueOf(getCurPage()));
        return paramsMap;
    }
}
